<?php

class Potential extends AppModel
{

    var $name = 'Potential';
    var $displayField = 'id';
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Product' => array(
            'className' => 'Product',
            'foreignKey' => 'product_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Leadstage' => array(
            'className' => 'Leadstage',
            'foreignKey' => 'leadstage_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Dealstage' => array(
            'className' => 'Dealstage',
            'foreignKey' => 'dealstage_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Employee' => array(
            'className' => 'Employee',
            'foreignKey' => 'employee_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Assignedtoemployee' => array(
            'className' => 'Employee',
            'foreignKey' => 'assignedtoemployee_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Location' => array(
            'className' => 'Location',
            'foreignKey' => 'location_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
    var $hasMany = array(
        'Potentialactivity' => array(
            'className' => 'Potentialactivity',
            'foreignKey' => 'potential_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Task' => array(
            'className' => 'Task',
            'foreignKey' => 'potential_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
    var $validate = array(
        'user_id' => array(
            'numeric' => array(
                'rule' => 'numeric',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'Please choose a user'
            ),
            'greaterThan' => array(
                'rule' => array('comparison', '>=', 1),
                'message' => 'Please choose a lead stage'
            )
        ),
        'product_id' => array(
            'numeric' => array(
                'rule' => 'numeric',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'Please choose a product'
            ),
            'greaterThan' => array(
                'rule' => array('comparison', '>=', 1),
                'message' => 'Please choose a lead stage'
            )
        ),
        'type' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'Please choose potential type'
            )
        ),
        'dateenquired' => array(
            'date' => array(
                'required' => true,
                'allowEmpty' => false,
                'rule' => array('date', 'dmy'),
                'message' => 'Please enter/choose a valid date in dd-mm-yyyy format'
            )
        ),
        'leadstage_id' => array(
            'numeric' => array(
                'rule' => 'numeric',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'Please choose a lead stage'
            ),
            'greaterThan' => array(
                'rule' => array('comparison', '>=', 1),
                'message' => 'Please choose a lead stage'
            )
        ),
        'dealstage_id' => array(
            'numeric' => array(
                'rule' => 'numeric',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'Please choose a deal stage'
            ),
            'greaterThan' => array(
                'rule' => array('comparison', '>=', 1),
                'message' => 'Please choose a lead stage'
            )
        ),
        'employee_id' => array(
            'numeric' => array(
                'rule' => 'numeric',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'Please choose a lead stage'
            ),
            'greaterThan' => array(
                'rule' => array('comparison', '>=', 1),
                'message' => 'Please choose a lead stage'
            )
        ),
        'assignedtoemployee_id' => array(
            'numeric' => array(
                'rule' => 'numeric',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'To which employee this potential be assigned to? Please choose one'
            ),
            'greaterThan' => array(
                'rule' => array('comparison', '>=', 1),
                'message' => 'To which employee this potential be assigned to? Please choose one'
            )
        ),
        'location_id' => array(
            'numeric' => array(
                'rule' => 'numeric',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'Please choose a location'
            ),
            'greaterThan' => array(
                'rule' => array('comparison', '>=', 1),
                'message' => 'Please choose a location'
            )
        )
    );

    function beforeSave()
    {
        parent::beforeSave();
        /*
         * format the nextfollowupdate and duedate column according to the yyyy-dd-mmy H:i:s format
         */
        if( !empty($this->data[$this->alias]['dateenquired']) )
        {
            $this->data[$this->alias]['dateenquired'] = date('Y-m-d H:i:s', strtotime($this->data[$this->alias]['dateenquired']));
        }
        return true;
    }

}

?>